/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.profwizard;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.panteleyev.blogclient.SupportedService;
import org.panteleyev.wordpress.WordPressUserProfile;

final class ProfileTypeSelectionPanel extends JPanel implements IWizardPage {

    private SupportedService selectedService = SupportedService.LIVEJOURNAL_COM;

    /** Creates new form ProfileTypeSelectionPanel */
    public ProfileTypeSelectionPanel() {
        initComponents();

        ljPanel.add(Box.createHorizontalGlue());

        liveJournalRadio.setSelected(true);

        ljServiceList.setModel(new DefaultComboBoxModel<SupportedService>(SupportedService.getLJServices()));
        ljServiceList.setMaximumSize(ljServiceList.getPreferredSize());
    }

    SupportedService getSelectedService() {
        if (liveJournalRadio.isSelected()) {
            return (SupportedService)ljServiceList.getSelectedItem();
        } else {
            return selectedService;
        }
    }

    @Override
    public boolean isNextAllowed() {
        if (wordPressCustomRadio.isSelected() && (getXmlRpc().length() == 0)) {
            return false;
        } else {
            return true;
        }
    }

    String getXmlRpc() {
        if (wordPressRadio.isSelected()) {
            return WordPressUserProfile.XMLRPC_DEFAULT;
        } else {
            return xmlRpcEdit.getText();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        blogTypeButtonGroup = new javax.swing.ButtonGroup();
        textPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        radioPanel = new javax.swing.JPanel();
        ljPanel = new javax.swing.JPanel();
        liveJournalRadio = new javax.swing.JRadioButton();
        ljServiceList = new javax.swing.JComboBox();
        blogspotRadio = new javax.swing.JRadioButton();
        wordPressRadio = new javax.swing.JRadioButton();
        wordPressCustomRadio = new javax.swing.JRadioButton();
        xmlRpcPanel = new javax.swing.JPanel();
        xmlRpcLabel = new javax.swing.JLabel();
        xmlRpcEdit = new javax.swing.JTextField();
        flickrRadio = new javax.swing.JRadioButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        textPanel.setAlignmentX(0.1F);

        jLabel1.setText(L10N.PROFILE_TYPE_PANEL_TEXT.s());
        textPanel.add(jLabel1);

        add(textPanel);

        radioPanel.setAlignmentX(0.0F);
        radioPanel.setLayout(new javax.swing.BoxLayout(radioPanel, javax.swing.BoxLayout.Y_AXIS));

        ljPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        ljPanel.setLayout(new javax.swing.BoxLayout(ljPanel, javax.swing.BoxLayout.X_AXIS));

        blogTypeButtonGroup.add(liveJournalRadio);
        liveJournalRadio.setText("LiveJournal");
        liveJournalRadio.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        liveJournalRadio.setMargin(new java.awt.Insets(2, 0, 2, 2));
        liveJournalRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                liveJournalRadioActionPerformed(evt);
            }
        });
        ljPanel.add(liveJournalRadio);

        ljServiceList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        ljPanel.add(ljServiceList);

        radioPanel.add(ljPanel);

        blogTypeButtonGroup.add(blogspotRadio);
        blogspotRadio.setText("Google");
        blogspotRadio.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        blogspotRadio.setMargin(new java.awt.Insets(2, 0, 2, 2));
        blogspotRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blogspotRadioActionPerformed(evt);
            }
        });
        radioPanel.add(blogspotRadio);

        blogTypeButtonGroup.add(wordPressRadio);
        wordPressRadio.setText("wordpress.com");
        wordPressRadio.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        wordPressRadio.setMargin(new java.awt.Insets(2, 0, 2, 2));
        wordPressRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordPressRadioActionPerformed(evt);
            }
        });
        radioPanel.add(wordPressRadio);

        blogTypeButtonGroup.add(wordPressCustomRadio);
        wordPressCustomRadio.setText("WordPress Generic");
        wordPressCustomRadio.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        wordPressCustomRadio.setMargin(new java.awt.Insets(2, 0, 2, 2));
        wordPressCustomRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordPressCustomRadioActionPerformed(evt);
            }
        });
        radioPanel.add(wordPressCustomRadio);

        xmlRpcPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
        xmlRpcPanel.setLayout(new javax.swing.BoxLayout(xmlRpcPanel, javax.swing.BoxLayout.LINE_AXIS));

        xmlRpcLabel.setLabelFor(xmlRpcEdit);
        xmlRpcLabel.setText("XMLRPC:");
        xmlRpcLabel.setEnabled(false);
        xmlRpcPanel.add(xmlRpcLabel);

        xmlRpcEdit.setColumns(30);
        xmlRpcEdit.setToolTipText("<html>\nPut xmlrpc.php path here. It's usually placed in the root of the server.<br>\nExample:<br>\nhttp://blog.domain.com/xmlrpc.php\n</html>");
        xmlRpcEdit.setEnabled(false);
        xmlRpcPanel.add(xmlRpcEdit);

        radioPanel.add(xmlRpcPanel);

        blogTypeButtonGroup.add(flickrRadio);
        flickrRadio.setText("Flickr");
        flickrRadio.setMargin(new java.awt.Insets(2, 0, 2, 2));
        flickrRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                flickrRadioActionPerformed(evt);
            }
        });
        radioPanel.add(flickrRadio);

        add(radioPanel);
    }// </editor-fold>//GEN-END:initComponents

private void wordPressCustomRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordPressCustomRadioActionPerformed
        xmlRpcEdit.setEnabled(true);
        xmlRpcLabel.setEnabled(true);
        ljServiceList.setEnabled(false);
        selectedService = SupportedService.WORDPRESS;
}//GEN-LAST:event_wordPressCustomRadioActionPerformed

private void liveJournalRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_liveJournalRadioActionPerformed
        xmlRpcEdit.setEnabled(false);
        xmlRpcLabel.setEnabled(false);
        ljServiceList.setEnabled(true);
}//GEN-LAST:event_liveJournalRadioActionPerformed

private void blogspotRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blogspotRadioActionPerformed
        xmlRpcEdit.setEnabled(false);
        xmlRpcLabel.setEnabled(false);
        ljServiceList.setEnabled(false);
        selectedService = SupportedService.BLOGGER_COM;
}//GEN-LAST:event_blogspotRadioActionPerformed

private void wordPressRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordPressRadioActionPerformed
        xmlRpcEdit.setEnabled(false);
        xmlRpcLabel.setEnabled(false);
        ljServiceList.setEnabled(false);
        selectedService = SupportedService.WORDPRESS_COM;
}//GEN-LAST:event_wordPressRadioActionPerformed

private void flickrRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_flickrRadioActionPerformed
        xmlRpcEdit.setEnabled(false);
        xmlRpcLabel.setEnabled(false);
        ljServiceList.setEnabled(false);
        selectedService = SupportedService.FLICKR;
}//GEN-LAST:event_flickrRadioActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup blogTypeButtonGroup;
    private javax.swing.JRadioButton blogspotRadio;
    private javax.swing.JRadioButton flickrRadio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton liveJournalRadio;
    private javax.swing.JPanel ljPanel;
    private javax.swing.JComboBox ljServiceList;
    private javax.swing.JPanel radioPanel;
    private javax.swing.JPanel textPanel;
    private javax.swing.JRadioButton wordPressCustomRadio;
    private javax.swing.JRadioButton wordPressRadio;
    private javax.swing.JTextField xmlRpcEdit;
    private javax.swing.JLabel xmlRpcLabel;
    private javax.swing.JPanel xmlRpcPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getPrevPage() {
        return null;
    }

    @Override
    public String getNextPage() {
        if (flickrRadio.isSelected()) {
            return NewProfileWizard.FLICKR_AUTH_PAGE;
        } else {
            return NewProfileWizard.USER_DATA_PAGE;
        }
    }

}
